<%@ page contentType="text/html; charset=UTF-8"%>
<%@page import="java.util.List"%>
<%@page import="com.sinodata.bsm.common.vo.Property"%>
<%@page import="com.sinodata.bsm.common.vo.CollectField"%>
<%@page import="com.sinodata.bsm.common.constants.PropertyConstants"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%
	String[] params = (String[])request.getAttribute("params");
	List<Property> propertys = (List<Property>)request.getAttribute("propertys");
	String[] props =(String[]) request.getAttribute("props");
 %>
<html>
  <head>
    <title>采集任务增加</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <meta http-equiv="pragma" content="no-cache"/>
	<meta http-equiv="cache-control" content="no-cache"/>
	<meta http-equiv="expires" content="0"/>  
	<%@ include file="../include/common.jsp"%>
	<%@ include file="../include/jbox.jsp"%>
	<%@ include file="../include/crontab.jsp"%>
	<%@ include file="../include/timeUnit.jsp"%>
	<%@ include file="../include/table.jsp"%>
	<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/style/default/bsm/sub_menu.css" />
	<style type="text/css">
		#task tbody td{
			white-space:normal;
		}
		
		.tdTitle{
				text-align:center;
				border: 1px solid #D6D3D6;
				line-height: 26px;
				height: 32px;
				color: black;
				background-color: #F6F6F6;
				color: black;
				font-weight: normal;
				overflow: hidden;
		}
	</style>
	<script type="text/javascript">
		var weeks = new Array();  //保存所有的周期采集对象
		function	changeScheduleType(id,i){
			var typeId = $("#"+id+"").val();
			if(typeId==1){
				$('#week'+i+'').css('display','block');  //周期
				$('#timing'+i+'').css('display','none');
			}else if(typeId==2){
				$('#week'+i+'').css('display','none');
				$('#timing'+i+'').css('display','block');
			}
		}
		function sub(){
			var returnFlag = true;
			var schedules = $("#task tbody tr[name='scheduleRow']"); 
			if(schedules.length<=0){
				getTopWin(window).getMessageBox().alert("调度方式必须包含一项","提示");
				return false;
			}else{
				var weekCount = 0; //周期设置只能一个
				$.each(schedules,function(index,ob){
					var type = $($(ob).children('td')[1]).children('select').val(); //采集类型
					if(type==1){
						var week = weeks[index];
						if(week.results()==0){
							getTopWin(window).getMessageBox().alert("第"+(index+1)+"个调度时间不能为0","提示");
							returnFlag = false;
							return false;
						}
						weekCount++;
						if(weekCount>1){
							getTopWin(window).getMessageBox().alert("周期采集只能有一个","提示");
							returnFlag = false;
							return false;
						}
					}
				});
			}
			
			var collectParam = $("input[name='paramValue']");
			$.each(collectParam,function(index,ob){
				if($(ob).val()==''){
					getTopWin(window).getMessageBox().alert("采集参数不能为空","提示");
					$(this).focus();
					returnFlag = false;
					return false;
				}
			});
			
			var collectField = $("input[name='fieldName']");
			var empty = 0;
			$.each(collectField,function(index,ob){
				var val = $(ob).val();
				if(val==''){
					empty++;
				}
				
				if(empty==collectField.length){
					getTopWin(window).getMessageBox().alert("采集字段必须有一项不能为空","提示");
					returnFlag = false;
					return false;
				}
			});
			
			if(returnFlag == false){
				return false;
			}
			
			var scheduleArray ="["; //采集调度
			$.each(schedules,function(index,ob){
				var type = $($(ob).children('td')[1]).children('select').val(); //采集类型
				var time = $($(ob).children('td')[2]).children('div').children('div')[1]; //定时采集
				var schedule = "{'scheduleType':"+type+",";
				if(type==1){
					schedule+="'scheduleParam':"+weeks[index].results();
				}else if(type==2){
					var id =$(time).attr('id');
					var value = Crontab.Data.get.getDataByKey(""+id+"");
					schedule+="'scheduleParam':"+value;
				}	
					schedule+="}";
					if(index!=schedules.length-1){
						schedule+=",";
					}
					scheduleArray+=schedule;
			});
			scheduleArray+="]";
			
			
			var collectParamName = $("span[name='paramName']"); //采集参数
			var collectParamArry = "[";
			$.each(collectParamName,function(index,ob){
				var param = "{'paramName':'"+$(ob).text()+"'}";
				if(index!=collectParamName.length-1){
					param+=',';
				}
				collectParamArry+=param;
			});
			collectParamArry+="]";
			
			var props = $("span[name='prop']");
			var collectFieldArray  = "[";
			$.each(props,function(index,ob){
				var field = "{'prop':"+$(ob).attr("idValue")+",'fieldName':'"+$(collectField[index]).val().trim()+"'}";
				if(index!=props.length-1){
					field+=",";
				}
				collectFieldArray+=field;
			});
			collectFieldArray+="]";
			
			var taskForm = $('#taskForm');
			taskForm.attr('action','collect-task!save.action?collectSchedules='+scheduleArray+'&paramNames='+collectParamArry+'&collectFields='+collectFieldArray);
			return true;
		}
		
		function addRow(){
			var indexValue =weeks.length;
			var tr = $('#copyTask').html().replace('del(i)','del('+indexValue+')')
										  .replace('scheduleTypei','scheduleType'+(indexValue)+'')
								          .replace('changeScheduleType(this.id,i)','changeScheduleType(this.id,'+(indexValue)+')')
								          .replace('weeki','week'+(indexValue)+'')
								          .replace('timingi','timing'+(indexValue)+'');
			var task=$('#task tbody tr:last-child');
			task.before('<tr id="scheduleRow" name="scheduleRow" class="odd" delIndex="'+indexValue+'" >'+tr+'</tr>');
			var week = new SimpleTimeUnit("week"+(indexValue)+"");
				 	week.show();          
					week.reversal('60');
					weeks.push(week);	
			 Crontab.Method.initAddCrontab("timing"+indexValue+"","0 0 0 * * ?"); 
		}
		
		function del(iRow){
				var scheduleRow = $("#task tbody tr[name='scheduleRow']");
				if(scheduleRow.length>1){
					var n = 0;
					$.each(scheduleRow,function(index,ob){
						//删除保存在全局变量weeks里面的调度方式
						if($(ob).attr('delIndex')==iRow){
							$(ob).remove();
						}
						
						if(weeks[index]!=weeks[iRow]){
							weeks[n++] = weeks[index];
						}
					});
					weeks.length-=1
					
				}else{
					getTopWin(window).getMessageBox().alert("必须有一个调度方式","提示");
				}
	  }
	</script>
  </head>
  <body>
  	<form action="" id="taskForm" onsubmit="return sub();" method="post">
  		<h2 class="title-h2"  ><span><img src="${pageContext.request.contextPath}/style/default/bsm/images/arrow-down.gif"/></span>新增采集任务</h2>
  		<div id="collectorInfo">
			<table class="table_eidt" >
				<tbody>
					<tr>
						<td class="td_left"  style="width:150px">资源类别采集器</td>
						<td class="td_right" style="width:350px;">${collector.name}<input name="resId" type="hidden" value="${resId}" />
						<input name="collectorId" type="hidden" value="${collector.id}" /></td>
						<td class="td_left"  style="width:150px">是否保存</td>
						<td class="td_right">
							<input type="radio" value="0"  name="isSave"  />否 
							<input type="radio" value="1" checked="checked"  name="isSave"  />是
						</td>
					</tr>
				</tbody>
			</table>
		</div>
		<h2 class="title-h2" onclick="accordionDiv(this)" ><span><img src="${pageContext.request.contextPath}/style/default/bsm/images/arrow-down.gif"/></span>调度方式设置</h2>
  		<div id="scheduleInfo">
  			<div class ="toolbar" id="toolbar">
				<a href="javascript:;" class="page_add" onclick="addRow()" >新增一行</a>
			</div>
			<table class="display dataTable" id="task" cellpadding="0" cellspacing="0" > 
				<thead>
					<tr role="row">
						<th style="width:150px;">操作</th>
						<th style="width:350px;">调度方式</th>
						<th >调度时间</th>
					</tr>
				</thead>
				<tbody>
					<script >
						//设置定时采集为准许在页面显示多个
						Crontab.isClearDiv = false;
						Crontab.isSingleton = false;
					</script>
				<tr id="copyTask" style="display:none;" >
				 	<td style="text-align:center;" ><a href="#" onclick="del(i)"><img src='../style/default/bsm/images/delete.gif' title='删除' /></a></td>
				 	<td style="padding-left:4px;">
				 		<select id="scheduleTypei" name="scheduleType" style="width:220px;" onchange="changeScheduleType(this.id,i)" class="input_text">
							<option value="<%=PropertyConstants.PROPERTY_REFRESH_PERIOD %>" selected>周期采集</option>
							<option value="<%=PropertyConstants.PROPERTY_REFRESH_TIMER %>" >定时采集</option>
						</select>
				 	</td>
				 	<td >
				 	  <div name="scheduleTime" class="week-div" >
				 		<div id='weeki'  name="week" ></div>
				 		
				 		<div id="timingi" name="timing"  style="display:none;"></div>
				 			
					 </div>
				 	</td>
				 </tr>
				</tbody>
			</table>
  		</div>
  		<h2 class="title-h2" onclick="accordionDiv(this)" ><span><img src="${pageContext.request.contextPath}/style/default/bsm/images/arrow-down.gif"/></span>采集参数设置</h2>
		<div id="collectParamInfo" >
			<table class="table_eidt" >
				<tbody>
					<%
						for(int i=0;i<params.length;i++){
						String  paramName = params[i];
					%>
					 	<tr>
							<td class="td_left"  style="width:150px"  >
								<span class="span_alert">*</span><span id="paramName" name="paramName" ><%=paramName %></span>
							</td>
							<td class="td_right">
								<input type="text" name="paramValue"  class="input_text" style="width:350px;" />
							</td>
					   </tr>
					<% 
						}
					%>
				</tbody>
			</table>
		</div>
		<h2 class="title-h2" onclick="accordionDiv(this)"  ><span ><img  src="${pageContext.request.contextPath}/style/default/bsm/images/arrow-down.gif"/></span>采集字段映射设置</h2>
  		<div id="fieldInfo"  >
			<table class="display dataTable" cellpadding="0" cellspacing="0" >
				<thead>
					<tr role="row" >
						<th  style="width:150px">指标</th>
						<th>字段表达式</th>
					</tr>
				</thead>
				<tbody>
					<%
						for(int i=0;propertys!=null&&i<propertys.size();i++){
						 Property property = propertys.get(i);
						 if(property==null){
						 	continue;
						 }
					%>
						<tr class="<%=i%2==0?"odd":"even" %>">
							<td  style="width:150px;">
								<span class="span_alert">*</span> <span  name="prop" idValue="<%=property.getId() %>" ><%=property.getName() %></span>
							</td>
							<td >
								<input type="text"  class="input_text"  name="fieldName" maxlength="20" style="width:350px;" />
							</td>
						</tr>
				   <%	
					 }
				   %>
				   <tr class="odd">
					<td   colspan="2" style="text-align: left;" >
						<span style="color:red;">可参照字段：</span>
						<%
						for(int r = 0 ; props!=null&&r<props.length;r++){
							String str = props[r];
							
							out.println(str);
						}
						%>
					</td>
				</tr>	
				</tbody>
			</table>
		</div>
		<div class="btn-div" >
			<input type="submit" value="保存" class="btn-4" />
			<input type="button" value="返回" onclick="history.back()" class="btn-4" />
		</div>
  	</form>
  </body>
</html>
